import com.sun.tools.jconsole.JConsoleContext;

import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.ArrayList;
import java.util.Arrays;

public class MyArrayList implements IList {

    private int[] elem;

    @Override
    public String toString() {
        return "MyArrayList{" +
                "elem=" + Arrays.toString(elem) +
                ", usedSize=" + usedSize +
                '}';
    }

    private int usedSize;

    public  MyArrayList() {
        this.elem = new int[Constant.DEFAULT_CAPACITY];
    }
    private boolean isFull() {
        return usedSize == elem.length - 1;
    }

    private void grow() {
        Arrays.copyOf(this.elem, 2 * this.elem.length);
    }


    @Override
    public void add(int data) {
        if (isFull()) {
            grow();
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        if (isFull()) {
            grow();
        }
        if (pos == usedSize) {
            elem[pos] = data;
            this.usedSize++;
            return;
        }
        checkPos(pos, Constant.POS_ILLEGALITY + pos);
        for (int i = usedSize - 1; i >= pos; --i) {
            elem[i + 1] = elem[i];
        }
        this.elem[pos] = data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; ++i) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; ++i) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty() {
        return this.usedSize == 0;
    }

    private void checkPos(int pos, String msg) {
        if (pos < 0 || pos >= usedSize) {
            throw new PosIllegalityException(msg);
        }
    }

    @Override
    public int get(int pos) {
        if (isEmpty()) {
            throw new EmptyListException();
        }
        checkPos(pos, Constant.POS_ILLEGALITY + pos);
        return this.elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        if (isEmpty()) {
            throw new  EmptyListException();
        }
        checkPos(pos, Constant.POS_ILLEGALITY + pos);

        this.elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if (isEmpty()) {
            throw new EmptyListException();
        }
        int index = indexOf(toRemove);
        if (index == -1) {
            System.out.println("没有你要删除的数据");
            return;
        }
        for (int i = index; i < usedSize; ++i) {
            this.elem[i] = this.elem[i + 1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < this.usedSize; ++i) {
            this.elem[i] = 0;
        }
        this.usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; ++i) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }
}
